'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url client/timedTask/pub/autoCancelOrder 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		let time = Date.now();

		try {
			let places = await vk.baseDao.select({
				dbName: "bidding-places", // 表名

				getCount: false, // 是否需要同时查询满足条件的记录总数量，默认false
				getMain: true, // 是否只返回rows数据，默认false
				pageIndex: 1, // 当前第几页
				pageSize: -1, // 每页条数
				whereJson: { // 条件
					is_on: true,
					dateArr: _.neq(null),
					bingtimes: _.neq(null)
				}
			});

			for (var i = 0; i < places.length; i++) {
				let place = places[i];
				let overSession = await vk.baseDao.select({
					dbName: "bidding-session",

					pageIndex: 1,
					getMain: true,
					pageSize: -1,
					whereJson: {
						place_id: place._id,
						end: _.lte(time)
					}
				});
				if (!vk.pubfn.isNull(overSession)) {
					const _ids = overSession.map(x => x._id);
					let num = await vk.baseDao.update({
						dbName: "bidding-session", // 表名

						whereJson: { // 条件
							_id: _.in(_ids)
						},
						dataJson: { // 需要修改的数据
							status: 3
						}
					});
				}

				//符合当前当前拍卖的时段
				let selectRes = await vk.baseDao.select({
					dbName: "bidding-session",

					pageIndex: 1,
					getMain: true,
					pageSize: -1,
					whereJson: {
						place_id: place._id,
						start: _.lte(time),
						end: _.gte(time)
					}
				});


				if (vk.pubfn.isNotNull(selectRes) && selectRes.length > 0) {
					for (var j = 0; j < selectRes.length; j++) {
						let now = selectRes[j];
						console.log('-----selectRes')
						console.log(now)
						console.log('-----selectRes')
						let num = await vk.baseDao.updateById({
							dbName: 'bidding-session',

							id: now._id,
							dataJson: {
								status: 2
							}
						});
						let num2 = await vk.baseDao.update({
							dbName: 'bidding-session',

							whereJson: {
								place_id: place._id,
								status: 1
							},
							dataJson: {
								status: 0
							}
						});
					}

				} else {
					//	console.log('no in')
					//找出待命和进行中的
					let stand = await vk.baseDao.select({
						dbName: "bidding-session",

						pageIndex: 1,
						getMain: true,
						pageSize: -1,
						whereJson: {
							place_id: place._id,
							status: _.in([1, 2])
						}
					});
					console.log(`stand`)
					console.log(stand)
					console.log(`stand`)
					console.log(stand)
					if (vk.pubfn.isNull(stand)) {
						let session = await vk.baseDao.select({
							dbName: "bidding-session", // 表名

							getOne: true, // 是否需要同时查询满足条件的记录总数量，默认false
							getMain: true, // 是否只返回rows数据，默认false
							pageIndex: 1, // 当前第几页
							pageSize: -1, // 每页条数
							whereJson: { // 条件
								status: 0,
								place_id: place._id,
								start: _.gte(time)
							},
							sortArr: [{
								"name": "start",
								"type": "asc"
							}],
						});
						console.log(`session`)
						console.log(session)
						console.log(`session`)
						//更新
						if (vk.pubfn.isNotNull(session)) {
							console.log(`u---session`)
							console.log(session._id)
							console.log(`u---session`)
							let num3 = await vk.baseDao.update({
								dbName: 'bidding-session',
								whereJson: {
									_id: session._id
								},
								dataJson: {
									status: 1
								}
							});
						}
					}
				}


			}

		}
		catch (error) {

			console.error(`transaction error`, error)
			return {
				code: -1,
				msg: "数据库写入异常,事务已回滚",
				err: {
					message: error.message,
					stack: error.stack
				}
			}
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}


}